package com.onluy.frameworkcloud.data.util;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class DatasetList<E> extends ArrayList<E>
  implements IDataset<E>
{
  private String serializableId;
  private String serializablePath;
  private boolean serializable;
  private boolean batchSerializable;
  private int batchPageSize;
  private int batchPageCount;

  public DatasetList()
  {
  }

  public DatasetList(List list)
  {
    addAll(list);
  }

  public String getSerializableId()
  {
    return this.serializableId;
  }

  public void setSerializableId(String serializableId)
  {
    this.serializableId = serializableId;
  }

  public String getSerializablePath()
  {
    return this.serializablePath;
  }

  public void setSerializablePath(String serializablePath)
  {
    this.serializablePath = serializablePath;
  }

  public boolean isSerializable()
  {
    return this.serializable;
  }

  public void setSerializable(boolean serializable)
  {
    this.serializable = serializable;
  }

  public boolean isBatchSerializable()
  {
    return this.batchSerializable;
  }

  public void setBatchSerializable(boolean batchSerializable)
  {
    this.batchSerializable = batchSerializable;
  }

  public int getBatchPageSize()
  {
    return this.batchPageSize;
  }

  public void setBatchPageSize(int batchPageSize)
  {
    this.batchPageSize = batchPageSize;
  }

  public int getBatchPageCount()
  {
    return this.batchPageCount;
  }

  public void setBatchPageCount(int batchPageCount)
  {
    this.batchPageCount = batchPageCount;
  }

  public Object get(int index, String name)
  {
    IData data = (IData)get(index);
    return data == null ? null : data.get(name);
  }

  public Object get(int index, String name, Object def)
  {
    Object value = get(index, name);
    return value == null ? def : value;
  }

  public String[] getNames()
  {
    return size() > 0 ? ((IData)get(0)).getNames() : null;
  }

  public IData toData()
    throws Exception
  {
    IData data = new DataMap();

    Iterator it = iterator();
    while (it.hasNext()) {
      IData element = (IData)it.next();
      Iterator iterator = element.keySet().iterator();
      while (iterator.hasNext()) {
        String key = (String)iterator.next();
        if (data.containsKey(key)) {
          List list = (List)data.get(key);
          list.add(element.get(key));
        } else {
          List list = new ArrayList();
          list.add(element.get(key));
          data.put(key, list);
        }
      }
    }

    return data;
  }

  public int count()
  {
    return size();
  }

  public void sort(String key, int keyType)
  {
    sort(key, keyType, 0);
  }

  public void sort(String key, int keyType, int order)
  {
    Object[] datas = (Object[])toArray();
    DataComparator c = new DataComparator(key, keyType, order);
    Arrays.sort(datas, c);

    List list = Arrays.asList(datas);

    clear();
    addAll(list);
  }

  public void sort(String key1, int keyType1, String key2, int keyType2)
  {
    sort(key1, keyType1, 0);
    sort(key2, keyType2, 0, key1, keyType1);
  }

  public void sort(String key1, int keyType1, int order1, String key2, int keyType2, int order2)
  {
    sort(key1, keyType1, order1);
    sort(key2, keyType2, order2, key1, keyType1);
  }

  private void sort(String key, int type, int order, String fix, int fixType)
  {
    Object[] datas = (Object[])toArray();

    DataComparator c = new DataComparator(key, type, order);

    if (fix == null) {
      Arrays.sort(datas, c);
    } else {
      int[] marks = Anchor.mark(this, fix, fixType);

      int pre = 0; for (int i = 1; i < marks.length; i++) {
        Arrays.sort(datas, pre, marks[i], c);
        pre = marks[i];
      }
    }
    List list = Arrays.asList(datas);

    clear();
    addAll(list);
  }
}